<html>
<head><meta charset="utf-8"><title>how does auto-deref work for AsRef · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20does.20auto-deref.20work.20for.20AsRef.html">how does auto-deref work for AsRef</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214336949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20does%20auto-deref%20work%20for%20AsRef/near/214336949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20does.20auto-deref.20work.20for.20AsRef.html#214336949">(Oct 23 2020 at 16:45)</a>:</h4>
<p><a href="https://doc.rust-lang.org/std/convert/trait.AsRef.html">https://doc.rust-lang.org/std/convert/trait.AsRef.html</a></p>
<p>This isn't a lang item, where in the compiler does it figure out that it can use .as_ref to autoderef? I can't find any reference to AsRef in rustc_trait_selection or rustc_middle</p>



<a name="214337001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20does%20auto-deref%20work%20for%20AsRef/near/214337001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20does.20auto-deref.20work.20for.20AsRef.html#214337001">(Oct 23 2020 at 16:45)</a>:</h4>
<p>auto deref uses Deref, not AsRef</p>



<a name="214337331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20does%20auto-deref%20work%20for%20AsRef/near/214337331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20does.20auto-deref.20work.20for.20AsRef.html#214337331">(Oct 23 2020 at 16:48)</a>:</h4>
<p>So the answer is "poorly"? <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="214353451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20does%20auto-deref%20work%20for%20AsRef/near/214353451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20does.20auto-deref.20work.20for.20AsRef.html#214353451">(Oct 23 2020 at 19:03)</a>:</h4>
<p>oh i misread the docs: <code>**AsRef auto-dereferences if the inner type is a reference or a mutable reference (e.g.: foo.as_ref() will work the same if foo has type &amp;mut Foo or &amp;&amp;mut Foo)**</code></p>



<a name="214368786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/how%20does%20auto-deref%20work%20for%20AsRef/near/214368786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/how.20does.20auto-deref.20work.20for.20AsRef.html#214368786">(Oct 23 2020 at 21:34)</a>:</h4>
<p>Yup, the "inner" de-referencing is also just a normal trait impl on <code>&amp;</code>/<code>&amp;mut</code> etc.. Roughly like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;'</span><span class="nb">_</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">AsRef</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">&amp;'</span><span class="nb">_</span><span class="w"> </span><span class="n">T</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="n">T</span>: <span class="nb">AsRef</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="o">?</span><span class="nb">Sized</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">U</span>: <span class="o">?</span><span class="nb">Sized</span><span class="p">,</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>